package nuc.soft.A01.jzs.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.user.Teacher;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.services.TeacherService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

	@Autowired
	private TeacherService service;

	@RequestMapping("/showsearch")
	public String getSearchTeacher(HttpServletRequest request,
			HttpServletResponse response, String searchname) {
		ArrayList<Teacher> allTeacher = service.getSearchSys(searchname);
		request.setAttribute("teachers", allTeacher);
		return "teacher/teacherindex";
	}

	@RequestMapping("/showall")
	public String getAllTeacher(HttpServletRequest request,
			HttpServletResponse response) {
		ArrayList<Teacher> allTeacher = service.getAllTeacher();
		request.setAttribute("teachers", allTeacher);
		return "teacher/teacherindex";
	}

	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) {

		return "teacher/add";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			Teacher Teacher) {
		service.insert(Teacher);
		return "forward:/teacher/showall";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) {
		Teacher Teacher = service.getTeacherById(theid);
		request.setAttribute("editteacher", Teacher);
		return "/teacher/edit";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, Teacher teacher, int tochoose) {
		if (tochoose == 1) {
			teacher.setPass(teacher.getTeacherid());
		}
		service.update(teacher);
		return "forward:/teacher/showall";
	}
}
